﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Com.Toook.Common;
using System.Data.OleDb;

namespace Com.Toook.DAL
{
    public enum RecommendType
    {
        Article,
        Joke,
        Movie
    }
    /// <summary>
    /// Article/Joke/Movie 分类的默认值60/50/30
    /// </summary>
    public enum RecommendId
    {
        Article = 60,
        Joke = 50,
        Movie = 30
    }
    /// <summary>
    ///RecommendData 的摘要说明
    /// </summary>
    public class RecommendData
    {
        public RecommendData()
        {
        }
        /// <summary>
        /// 获取分类推荐数据
        /// </summary>
        /// <param name="typeName">大分类名称：movie/joke/article</param>
        /// <param name="defaultTypeId">url中的type参数不合法时的默认大分类值：30/50/60</param>
        /// <param name="paramTypeId">url中的type参数：Request["type"]</param>
        /// <param name="showCount">显示多少条数据</param>
        /// <param name="isBottom">侧边显示、还是底部显示</param>
        /// <returns></returns>
        public static DataTable GetRecommend(RecommendType typeName, RecommendId defaultTypeId, string paramTypeId, int showCount, bool isBottom)
        {
            int type = ParamHelper.ParseIntPositive(paramTypeId);
            int defaultId = (int)defaultTypeId;
            string name = typeName.ToString().ToLower();
            int total = 1;
            DataRow row = null;
            //类型不为空且是数字，设置类型条件。否则不加类型条件。
            if (type > 0 && type != defaultId)
            {
                row = PageData.SelectParamRow("typeCode='" + name + type.ToString() + "'");
            }
            if (row == null)//没有找到，类型不对.设置为默认
            {
                row = PageData.SelectParamRow("typeCode='" + name + "'");
                type = defaultId;
            }
            
            total = PageData.GetTotalCount(type);
            int start = 0;
            if (total > showCount)
            {
                int num = isBottom ? 59317 : 911733;//侧边、底部生成不同种子值
                Random rand = new Random(DateTime.Now.Second + num);
                start = rand.Next(total - showCount);
            }
            start++;//随机数从0开始，页面从1开始。所以要加 1
            string pageCondition;
            if (type == defaultId)//总的分页根据id分
            {
                pageCondition = " id>=" + start.ToString() + " and id<" + (start + showCount).ToString();
            }
            else//指定类型的分页
            {
                pageCondition = PageData.GetPageStartEndCondition(type, start, showCount);
            }

            string sql = "select id, typePageId, title from Publish" + typeName.ToString() + " where " + pageCondition;
            OleDbConnection conn = AccessHelper.GetConnection();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            OleDbDataReader reader = null;
            DataTable table = new DataTable();
            try
            {
                conn.Open();
                reader = cmd.ExecuteReader();
                table.Load(reader);
            }
            finally
            {
                AccessHelper.CloseDbResource(conn, reader);
            }

            return table;
        }
    }
}